﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.DataAccess;

    /// <summary>
    /// CommonProcSV partial 
    /// </summary>	
    public partial class CommonProcSV 
	{	
		internal BaseStrategy Select()
		{
			return new CommonProcSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CommonProcSVImpementStrategy : BaseStrategy
	{
		public CommonProcSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			CommonProcSV bpObj = (CommonProcSV)obj;

            try
            {
                if (bpObj.Data == null)
                {
                    throw new Exception("传入数据为空");
                }
                if (string.IsNullOrEmpty(bpObj.Data.ProcName))
                    throw new Exception("传入参数[ProcName]为空");

                //构造参数
                DataParamList para = new DataParamList();
                if (!string.IsNullOrEmpty(bpObj.Data.Parm1Name))
                {
                    para.Add(DataParamFactory.CreateInput(bpObj.Data.Parm1Name, bpObj.Data.Parm1Value, DbType.String));
                }
                if (!string.IsNullOrEmpty(bpObj.Data.Parm2Name))
                {
                    para.Add(DataParamFactory.CreateInput(bpObj.Data.Parm2Name, bpObj.Data.Parm2Value, DbType.String));
                }
                if (!string.IsNullOrEmpty(bpObj.Data.Parm3Name))
                {
                    para.Add(DataParamFactory.CreateInput(bpObj.Data.Parm3Name, bpObj.Data.Parm3Value, DbType.String));
                }
                if (!string.IsNullOrEmpty(bpObj.Data.Parm4Name))
                {
                    para.Add(DataParamFactory.CreateInput(bpObj.Data.Parm4Name, bpObj.Data.Parm4Value, DbType.String));
                }
                if (!string.IsNullOrEmpty(bpObj.Data.Parm5Name))
                {
                    para.Add(DataParamFactory.CreateInput(bpObj.Data.Parm5Name, bpObj.Data.Parm5Value, DbType.String));
                }

                DataSet ds = null;
                DataAccessor.RunSP(DataAccessor.GetConn(),bpObj.Data.ProcName, para,out ds);
                //将Datatable转换为Dictionary
                //C#中有hashtable和Dictionary,hashtable的key排序是无序的,Dictionary的key排序是有序的
                List<Dictionary<string, object>> retlist = new List<Dictionary<string, object>>();
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Dictionary<string, object> retrow = new Dictionary<string, object>();
                        foreach (DataColumn col in ds.Tables[0].Columns)
                        {
                            object val = row[col.ColumnName];
                            retrow.Add(col.ColumnName, val);
                        }
                        retlist.Add(retrow);
                    }
                }

                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}